package com.rain.cy.business.handler;

import cn.hutool.core.thread.ThreadUtil;
import com.rain.cy.business.constant.CommonConstant;
import com.rain.cy.business.handler.base.BaseProcessHandler;
import com.rain.cy.business.handler.base.ProcessHandler;
import com.rain.cy.business.model.dto.UserProcessDataDTO;
import com.rain.cy.business.model.entity.UserFeedbackEntity;
import com.rain.cy.business.model.entity.UserScoreEntity;
import com.rain.cy.business.model.json.BaseProcessDataJson;
import com.rain.cy.business.model.json.ProcessFourOfDataJson;
import com.rain.cy.business.model.vo.ItemRecordVO;
import com.rain.cy.business.service.ActionCountService;
import com.rain.cy.business.service.UserFeedbackService;
import com.rain.cy.business.service.UserProcessStatusService;
import com.rain.cy.business.service.UserScoreService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author yu_chen
 * @since 2021/10/10 17:41
 */
@Component
public class ProcessFourHandler extends BaseProcessHandler {


    @Autowired
    private ActionCountService actionCountService;

    @Autowired
    private UserScoreService userScoreService;

    @Autowired
    private UserFeedbackService userFeedbackService;

    @Autowired
    private UserProcessStatusService userProcessStatusService;


    @Override
    public BaseProcessDataJson initProcessDataJson() {
        ProcessFourOfDataJson dataJson = new ProcessFourOfDataJson();
        dataJson.setSuggestToDoctor(new UserFeedbackEntity());
        dataJson.setRecordOfDrugResistance(new ItemRecordVO().setScore(0));
        dataJson.setRecordOfBadReaction(new ItemRecordVO().setScore(0));
        dataJson.setSuggestOfDoctor("");
        return dataJson;
    }

    @Override
    public void handler(UserProcessDataDTO userProcessDataDTO) {
        Integer userId = userProcessDataDTO.getUserId();
        ProcessFourOfDataJson processDataJson = (ProcessFourOfDataJson) userProcessDataDTO.getBaseProcessDataJson();
        boolean process1 = processDataJson.getRecordOfBadReaction() != null && StringUtils.isNotEmpty(processDataJson.getRecordOfBadReaction().getRecordContent());
        boolean process2 = processDataJson.getRecordOfDrugResistance() != null && StringUtils.isNotEmpty(processDataJson.getRecordOfDrugResistance().getRecordContent());
        if (process1) {
            userProcessStatusService.updateUserProcessStatus(userId, "4001");
        } else {
            userProcessStatusService.updateUserProcessStatusForDisable(userId, "4001");
        }
        if (process2) {
            userProcessStatusService.updateUserProcessStatus(userId, "4002");
        } else {
            userProcessStatusService.updateUserProcessStatusForDisable(userId, "4002");
        }
        if (process1 && process2) {
            userProcessStatusService.updateUserProcessStatus(userId, "50");
        }
        String suggestOfDoctor = processDataJson.getSuggestOfDoctor();
        if (StringUtils.isNotEmpty(suggestOfDoctor)) {
            //医生给患者反馈
            actionCountService.plusReceiveFeedbackNum(userProcessDataDTO.getUserId());
        }
        UserFeedbackEntity suggestToDoctor = processDataJson.getSuggestToDoctor();
        if (suggestToDoctor != null) {
            suggestToDoctor.setUserId(userId);
            suggestToDoctor.setProcessId(CommonConstant.PROCESS_ID_FOUR);
            suggestToDoctor.setFeedbackDay(userProcessDataDTO.getCreateDay());
            //触发分数计算
            userFeedbackService.saveFeedback(suggestToDoctor);
        }
        //触发分数计算
        ThreadUtil.execAsync(() -> userScoreService.getUserProcessScoreByPatientId(userProcessDataDTO.getUserId()));
    }
}
